﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NextAction.Domain.Abstract;
using NextAction.WebUI.Models;
using NextAction.Domain.Entities;
using System.Data.Objects.SqlClient;
using NextAction.Domain.Concrete;

namespace NextAction.WebUI.Controllers
{
    public class TaskController : AbstractGtdController
    {
        private GtdTaskFactory factory;
        
        public TaskController(IGtdEnityRepository gtdRepository)
            : base(gtdRepository)
        {
            factory = new GtdTaskFactory(repository);
        }

        public ViewResult Index(BucketType bucketType)
        {
            return View(factory.CreateIndexModel(bucketType));
        }

        public ViewResult IndexForProject(int projectID)
        {
            return View(factory.GetTasksForProject(projectID));
        }

        public ViewResult GtdTaskListData(BucketType bucketType)
        {
            return View(factory.CreateIndexModel(bucketType));
        }

        [HttpPost]
        public ViewResult GtdTaskListData(
            BucketType bucketType,
            string newTaskName)
        {
            if (!String.IsNullOrEmpty(newTaskName))
            {
                var task = factory.CreateTask(bucketType, newTaskName);
                repository.SaveGtdTask(task);
            }
            return View(factory.CreateIndexModel(bucketType));
        }

        [HttpPost]
        public void Move(int taskID, BucketType targetBucketType)
        {
            var task = factory.GetTask(taskID);
            task.TaskType = targetBucketType.GetGtdTaskType();
            task.ScheduleDate = targetBucketType.GetScheduleDate();
            repository.SaveGtdTask(task);
        }

        [HttpPost]
        public void Complete(int taskID)
        {
            var task = factory.GetTask(taskID);
            task.IsCompleted = true;
            repository.SaveGtdTask(task);
        }
        
        public ViewResult Edit(int taskID)
        {
            return View(factory.CreateEditModel(taskID));
        }

        [HttpPost]
        public ActionResult Edit(GtdTask gtdTask)
        {
            if (ModelState.IsValid)
            {
                repository.SaveGtdTask(gtdTask);
                return RedirectToAction("Index", "Collection");
            }
            else
            {
                return View(factory.CreateEditModel(gtdTask));
            }
        }

        public ViewResult Create(int? projectID, BucketType bucketType)
        {
            return View("Edit", factory.CreateEditModel(projectID, bucketType));
        }

        public JsonResult JsonData(GtdTaskType taskType)
        {

            var tasks = repository.GetGtdTasks(taskType);

            var formattedData = tasks.Select(m => new
            {
                TaskID = m.TaskID,
                Name = m.Name,
                ProjectID = m.ProjectID,
                ProjectName = m.ProjectName
            });

            return Json(formattedData, JsonRequestBehavior.AllowGet);
        }

    }
}
